package com.church.daoimpl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.church.dao.CurriculumDao;
import com.church.db.Curriculum;

public class CurriculumDaoImpl extends HibernateDaoSupport implements
		CurriculumDao {

	@Override
	public String add(Curriculum curriculum) {
		this.getHibernateTemplate().save(curriculum);
		return "success";
	}

	@Override
	public String update(Curriculum curriculum) {
	
		this.getHibernateTemplate().update(curriculum);
		return "success";
	}

	@Override
	public String remove(Curriculum curriculum) {
		this.getHibernateTemplate().delete(curriculum);
		return "success";
	}

	@Override
	public Curriculum findById(long id) {
		List<Curriculum> list = this.getHibernateTemplate().find(
				"from Curriculum as cu where cu.id=" + id);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<Curriculum> findByDate(Date date) {
		SimpleDateFormat dateformat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		List<Curriculum> list = this.getHibernateTemplate().find(
				"from Curriculum as cu where cu.date='"
						+ dateformat.format(date)+"' and cu.state!='3'");
		return list;
	}

	@Override
	public List<Curriculum> getAll() {
		SimpleDateFormat dateformat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		List<Curriculum> list = this.getHibernateTemplate().find("from Curriculum where state!='3'");
		return list;
	}

	@Override
	public List<Curriculum> getDateAfter(Date date) {
		// TODO Auto-generated method stub
		SimpleDateFormat dateformat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		List<Curriculum> list = this.getHibernateTemplate().find(
				"from Curriculum as cu where cu.date>='"
						+ dateformat.format(date)+"' and cu.state!='3'");
		return list;
	}

}
